package service;

import database.domain.ColumnInfo;
import number.UtilNumber;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.*;

/**
 * Description: dianxin zte 227 csv file into database
 * Created by E440 on 2017/5/22.
 */
public class DxZte227CsvIntoDatabase extends  SimpleCsv2Database {
    Set<String> specialSet=new HashSet<>();
    Map<String,String> cqiMap=new HashMap<>();

    public DxZte227CsvIntoDatabase(String dataBaseTableName, String configExcelFile, String configSheetName, String csvDataFile, Connection connection) throws InvalidFormatException, SQLException, IOException {
        super(dataBaseTableName, configExcelFile, configSheetName, csvDataFile, connection);

        String[] specialColumnNames={"小区CQI0使用数量","小区CQI1使用数量","小区CQI2使用数量","小区CQI3使用数量","小区CQI4使用数量","小区CQI5使用数量","小区CQI6使用数量","小区CQI7使用数量","小区CQI8使用数量","小区CQI9使用数量","小区CQI10使用数量","小区CQI11使用数量","小区CQI12使用数量","小区CQI13使用数量","小区CQI14使用数量","小区CQI15使用数量"};
        Collections.addAll(specialSet,specialColumnNames);

        for (String s : specialSet) {
            String numberString=s.replace("小区CQI","").replace("使用数量","");
            String csvDataHeadName="[FDD]CQI"+numberString+" 占比";
            cqiMap.put(s,csvDataHeadName);
        }
        logger.debug(specialSet);
        logger.debug(cqiMap);
    }


    /** 特殊字段 ，cqi字段
     * @param ps
     * @param psIndex
     * @param databaseColumnName
     * @param configCsvColumnName
     * @param columnInfo
     * @param dataLineList
     * @return
     * @throws SQLException
     */
    @Override
    public boolean extraSetParameter(PreparedStatement ps,int psIndex,String databaseColumnName,String configCsvColumnName,  ColumnInfo columnInfo,List<String> dataLineList) throws SQLException {
        if(specialSet.contains(databaseColumnName)){
            Long cqiValue;
            Map<String,Integer> csvHeadLineMap=csvHeadLineInfo.getHeadLineMap();
            int totalIndex=csvHeadLineMap.get("CQI总次数");
            Long totalCqi=Long.parseLong(dataLineList.get(totalIndex));
            logger.debug(totalCqi);
            cqiValue=calCqiValue(databaseColumnName,totalCqi,csvHeadLineMap,dataLineList);
            ps.setLong(psIndex,cqiValue);
            return true;
        }
        return false;
    }

    private Long calCqiValue(String dataColumnName,Long totalCqi,Map<String,Integer> csvHeadLineMap,List<String> dataLineList){
        Long cqiValue=new Long(-1);
        String csvDataHeadName=cqiMap.get(dataColumnName);
        int cqiIndex=csvHeadLineMap.get(csvDataHeadName);
        Double percent= UtilNumber.parseDouble(dataLineList.get(cqiIndex));
        cqiValue=Math.round(totalCqi*percent);
        return cqiValue;
    }
}
